package com.lms.classloader;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author: 李孟帅
 * @CreateTime: 2020-05-11 09:22
 * @Description: ${Description}
 */
public class MyClassLoader extends ClassLoader{

    @Override
    public Class<?> loadClass(String name) throws ClassNotFoundException {
        String filename = name.substring(name.lastIndexOf(".") + 1) + ".class";
        InputStream resourceAsStream = getClass().getResourceAsStream(filename);
        if (resourceAsStream == null) {
            return super.loadClass(name);
        }
        try {
            byte[] bytes = new byte[resourceAsStream.available()];
            int read = resourceAsStream.read(bytes);
            return defineClass(name,bytes,0,bytes.length);
        } catch (IOException e) {
            e.printStackTrace();
            throw new ClassNotFoundException(name);
        }

    }
}
